package com.ct.boss.demo.http;

/**
 * @author ChenTong
 * @version 1.0
 * @description 响应信息封装
 * @date 2020/6/15 21:28
 * @modified
 * eg.
 * {
 *  "data":{
 *      "user":{
 *          "name":"chentong,
 *          "age":"20"
 *      }
 *  },
 *  "meta":{
 *      "msg":"响应请求成功"，
 *      "status":200
 *  }
 * }
 **/
public class HttpResult {
    private Meta meta;
    private Object data;

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static HttpResult ok(){
        HttpResult result = new HttpResult();
        Meta meta = new Meta();
        meta.setMsg("响应成功");
        meta.setStatus(HttpStatus.SUCCESS.getCode());
        return result;
    }

    public static HttpResult ok(String msg){
        HttpResult result = new HttpResult();
        Meta meta = new Meta();
        meta.setMsg(msg);
        meta.setStatus(HttpStatus.SUCCESS.getCode());
        return result;
    }

    public static HttpResult ok(Object data){
        HttpResult result = new HttpResult();
        Meta meta = new Meta();
        meta.setMsg("响应成功");
        meta.setStatus(HttpStatus.SUCCESS.getCode());
        result.setData(data);
        result.setMeta(meta);
        return result;
    }

    public static HttpResult error(String msg){
        HttpResult result = new HttpResult();
        Meta meta = new Meta();
        meta.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR.getCode());
        meta.setMsg(msg);
        result.setMeta(meta);
        return result;
    }

    public static HttpResult error(String msg,int code){
        HttpResult result = new HttpResult();
        Meta meta = new Meta();
        meta.setStatus(code);
        meta.setMsg(msg);
        result.setMeta(meta);
        return result;
    }

}
